package chd.edu.spzx.manager.service.impl;

import chd.edu.spzx.common.exception.GuiguException;
import chd.edu.spzx.manager.mapper.SysMenuMapper;
import chd.edu.spzx.manager.mapper.SysRoleMenuMapper;
import chd.edu.spzx.manager.service.SysMenuService;
import chd.edu.spzx.manager.utils.MenuHelper;
import chd.edu.spzx.model.entity.system.SysMenu;
import chd.edu.spzx.model.entity.system.SysUser;
import chd.edu.spzx.model.vo.common.ResultCodeEnum;
import chd.edu.spzx.model.vo.system.SysMenuVo;
import chd.edu.spzx.utils.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @author Zhang Chaoliang
 * create 2023
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 菜单列表
     * @return
     */
    @Override
    public List<SysMenu> findNodes() {
        //1. 查询所有菜单，返回list集合
        List<SysMenu> sysMenuList = sysMenuMapper.findAll();
        if (CollectionUtils.isEmpty(sysMenuList))
            return null;

        //2. 调用工具类的方法，把返回的list集合封装成要求数据格式
        List<SysMenu> treeList = MenuHelper.buildTree(sysMenuList); //构建树形数据
        return treeList;
    }

    /**
     * 菜单添加
     * @param sysMenu
     * @return
     */
    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);

        //当新添加子菜单，把父菜单的isHalf变为半开状态 1
        updateSysRoleMenu(sysMenu);
    }

    /**
     * 当新添加子菜单，把父菜单的isHalf变为半开状态 1
     * @param sysMenu
     */
    private void updateSysRoleMenu(SysMenu sysMenu) {
        //获取当前添加菜单的父菜单
        SysMenu parentMenu = sysMenuMapper.selectParentMenu(sysMenu.getParentId());

        if(parentMenu != null) {
            //把父菜单的isHalf变为半开状态 1
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId());

            // 递归调用
            updateSysRoleMenu(parentMenu) ;
        }
    }

    /**
     * 菜单修改
     * @param sysMenu
     * @return
     */
    @Override
    public void update(SysMenu sysMenu) {
        sysMenuMapper.update(sysMenu);
    }

    /**
     * 菜单删除
     * @param id
     * @return
     */
    @Override
    public void removeById(Long id) {
        // 根据当前菜单的id，查询是否包含子菜单
        int count = sysMenuMapper.selectCountById(id);

        //判断count大于0，包含子菜单
        if (count>0){
            throw new GuiguException(ResultCodeEnum.NODE_ERROR);
        }
        //count等于0 ，直接删除
        sysMenuMapper.delete(id);
    }

    /**
     * 查询用户可以操作的菜单
     * @return
     */
    @Override
    public List<SysMenuVo> findMenusByUserId() {
        //1. 获取当前用户id
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();

        //2. 根据userId查询可以操作的菜单
        List<SysMenu> sysMenuList = sysMenuMapper.findMenuByUserId(userId);

        //3. 封装要求数据格式，返回
        List<SysMenu> list = MenuHelper.buildTree(sysMenuList);
        List<SysMenuVo> sysMenuVos = this.buildMenus(list);
        return sysMenuVos;
    }

    // 将List<SysMenu>对象转换成List<SysMenuVo>对象
    private List<SysMenuVo> buildMenus(List<SysMenu> menus) {

        List<SysMenuVo> sysMenuVoList = new LinkedList<>();
        for (SysMenu sysMenu : menus) {
            SysMenuVo sysMenuVo = new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }
}
